<?
include_class('releases');
include_class('m2');
include_class('band_members');
include_class('band_gps');
include_class('release_tracks');
if ($_GET['id']) {
	$r = Release::get($_GET['id']);
	if ($_GET['media_instance_id'] > 0) { 
		$mi = MediaInstance::get($_GET['media_instance_id']);
		if (!db::isError($mi) && !db::isError($r)) {
			if (($mi->getAreaID() == $r->getAVAreaID() || $mi->getAreaID() == $r->getPhotoAreaID()) && $r->isActive()) {
				$view = 'media';
				$media = new SectionTemplate;
				$media->file = "media_detail.php";
				$media->args['media'] = $mi;
				$media->args['exitURL'] = (USE_MOD_REWRITE) ? '/releases/' . $r->getID() . '/' : '/releases.php?id=' . $r->getID();
			}
		}
	} else if ($_GET['trackID']) { 
		$tr = ReleaseTrack::get($_GET['trackID']);
		if (!db::isError($tr)) {
			if ($tr->getReleaseID() == $r->getID()) {
				if (USE_MOD_REWRITE) {
					$releaseLink = '<a href="' . SITE_WEB_DIRECTORY . '/releases/' . $r->getID() . '">Return to release</a>';
				} else {
					$releaseLink = '<a href="' . SITE_WEB_DIRECTORY . '/releases.php?id=' . $r->getID() . '">Return to release</a>';
				}
				$view = 'track-detail';
				$detail = new SectionTemplate;
				$detail->file = "release_track_detail.php";
				$detail->args['track'] = $tr;
			}
		}		
	}
	
	if (!$view) {
		$view = 'detail';
		$detail = new SectionTemplate;
		$detail->file = "release_detail.php";
		$detail->args['release'] = $r;
	}
}

$rl = new ReleaseList;
$releaseList = $rl->get_releases();
if (count($releaseList) > 1) {
	$releases = array();
	foreach($releaseList as $r) {
		$releaseT = new SectionTemplate;
		$cov = $r->getCover();
		$releaseT->file = (is_object($cov)) ? "release_cover.php" : "release_name.php";
		$releaseT->args['release'] = $r;
		$releases[] = $releaseT;
	}
}

if (!$view) {
	$r = $rl->get_latest_release();
	if (!db::isError($r[0]) && is_object($r[0])) {
		$detail = new SectionTemplate;
		$detail->file = "release_detail.php";
		$detail->args['release'] = $r[0];
		$view = 'detail';
	}
}

$header = new SectionTemplate;
$header->file = "header.php";
$footer = new SectionTemplate;
$footer->file = "footer.php";